﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;

namespace ADB_QuanLyThuVien
{
    public class DAO_RequestForm
    {
        internal static bool AddBookRequest(string email, List<Book> lBook)
        {
            bool kq;
            if (isExsitForm(email) == true)
                kq = AddBookExistForm(email, lBook);
            else
            {
                RequestForm rq = new RequestForm();
                rq.IdForm = GetNextId();
                var config = Db4oEmbedded.NewConfiguration();
                config.Common.ObjectClass(typeof(Member)).CascadeOnUpdate(true);
                IObjectContainer db = Db4oEmbedded.OpenFile(config, QLTV_System.ConnectString);
                //Trỏ đến đối tượng độc giả
                Member mem = new Member();
                mem.Email = email;
                IObjectSet rReader = db.QueryByExample(mem);
                rq.Reader = (Member)rReader.Next();
                //Trỏ đến đối tượng sách
                for (int i = 0; i < lBook.Count; ++i)
                {
                    IObjectSet rBook = db.QueryByExample(lBook[i]);
                    BookRequest brq = new BookRequest();
                    brq.bookRequest = (Book)rBook.Next();
                    brq.DateRequest = DateTime.Today;
                    rq.lBookRequest.Add(brq);
                }
                db.Store(rq);
                db.Close();
                kq = true;
            }
            //var config = Db4oEmbedded.NewConfiguration();
            //config.Common.ObjectClass(typeof(BookRequest)).CascadeOnUpdate(true);
            //IObjectContainer db = Db4oEmbedded.OpenFile(config, QLTV_System.ConnectString);
            ////Trỏ đến đối tượng sách
            ////for (int i = 0; i < lBook.Count; ++i)
            ////{
            ////    IObjectSet rBook = db.QueryByExample(lBook[i]);
            ////    BookRequest brq = new BookRequest();
            ////    brq.bookRequest = (Book)rBook.Next();
            ////    brq.DateRequest = DateTime.Today;
            ////    rq.lBookRequest.Add(brq);
            ////}
            //db.Store(rq);
            //db.Close();
            return kq;
        }

        private static bool AddBookExistForm(string email, List<Book> lBook)
        {
            var config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(RequestForm)).CascadeOnUpdate(true);
            IObjectContainer db = Db4oEmbedded.OpenFile(config, QLTV_System.ConnectString);
            RequestForm rq = new RequestForm();
            rq.Reader.Email = email;
            IObjectSet result = db.QueryByExample(rq);
            RequestForm rqf = (RequestForm)result.Next();
            int dodai = rqf.lBookRequest.Count;
            for (int i = 0; i < lBook.Count; ++i)
            {
                int j;
                for (j = 0; j < dodai; ++j)
                    if (lBook[i].MaSach == rqf.lBookRequest[j].bookRequest.MaSach && rqf.lBookRequest[j].IsBorrow == false)
                        break;
                if (j == dodai)
                {
                    BookRequest brq = new BookRequest();
                    IObjectSet rBook = db.QueryByExample(lBook[i]);
                    brq.bookRequest = (Book)rBook.Next();
                    brq.DateRequest = DateTime.Today;
                    rqf.lBookRequest.Add(brq);
                }

            }
            for (int i = 0; i < rqf.lBookRequest.Count; ++i)
            {
                int j;
                for (j = 0; j < lBook.Count; ++j)
                    if (rqf.lBookRequest[i].bookRequest.MaSach == lBook[j].MaSach && rqf.lBookRequest[i].IsBorrow == false || rqf.lBookRequest[i].IsBorrow == true)
                        break;
                if (j == lBook.Count)
                    rqf.lBookRequest.RemoveAt(i);
            }
            db.Store(rqf);
            db.Close();
            return true;
        }

        private static RequestForm CreateNewRF(string email)
        {
            RequestForm rq = new RequestForm();
            rq.IdForm = GetNextId();
            var config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(Member)).CascadeOnUpdate(true);
            IObjectContainer db = Db4oEmbedded.OpenFile(config, QLTV_System.ConnectString);
            //Trỏ đến đối tượng độc giả
            Member mem = new Member();
            mem.Email = email;
            IObjectSet rReader = db.QueryByExample(mem);
            rq.Reader = (Member)rReader.Next();
            db.Close();
            return rq;
        }

        public static RequestForm SelectRFByEmail(string email)
        {
            RequestForm rq = new RequestForm();
            rq.Reader.Email = email;
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                IObjectSet result = db.QueryByExample(rq);
                rq = (RequestForm)result.Next();
            }
            return rq;
        }

        public static bool isExsitForm(string email)
        {
            bool kq = false;
            RequestForm rq = new RequestForm();
            rq.Reader.Email = email;
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                IObjectSet result = db.QueryByExample(rq);
                if (result.Count > 0)
                    kq = true;
            }
            return kq;
        }

        internal static string GetNextId()
        {
            string id = "RQ";
            DateTime now = DateTime.Now;
            id += now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00");
            return id;
        }

        internal static List<RequestForm> SelectAll()
        {
            List<RequestForm> lq = new List<RequestForm>();
            using (IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString))
            {
                IObjectSet result = db.QueryByExample(new RequestForm());
                for (int i = 0; i < result.Count; ++i)
                    lq.Add((RequestForm)result[i]);
            }
            return lq ;
        }

        internal static RequestForm SelectRFByID(string idForm)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<RequestForm> result = db.Query(delegate(RequestForm rq) // câu truy vấn theo nagative query
            {
                if (rq.IdForm == idForm)
                    return true;
                return false;
            }
            );
            RequestForm request = (RequestForm)result[0];
            db.Close();
            return request;
        }

        internal static RequestForm SelectRFByAcc(Account acc)
        {
            throw new NotImplementedException();
        }

        //internal static RequestForm SelectRFByEmail(string email)
        //{
        //    IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
        //    IList<RequestForm> result = db.Query(delegate(RequestForm rq) // câu truy vấn theo nagative query
        //    {
        //        if (rq.Reader.Email == email)
        //            return true;
        //        return false;
        //    }
        //    );
        //    RequestForm request = (RequestForm)result[0];
        //    db.Close();
        //    return request;
        //}

        internal static List<Book> SelectBookByEmail(string email)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            RequestForm rq = new RequestForm();
            rq.Reader.Email = email;
            IObjectSet result = db.QueryByExample(rq);
            //IList<RequestForm> result = db.Query(delegate(RequestForm rq) // câu truy vấn theo nagative query
            //{
            //    if (rq.Reader.Email == email)
            //        return true;
            //    return false;
            //});
            List<Book> list = new List<Book>();
            RequestForm rq1 = (RequestForm)result[0];
            for (int i = 0; i< rq1.lBookRequest.Count;++i)
            {
                if (rq1.lBookRequest[i].IsBorrow == false)
                    list.Add(rq1.lBookRequest[i].bookRequest);
            }
            db.Close();
            return list;
        }
    }
}
